package com.example.transation.txlconsumer.server.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.example.transation.txlcncommon.bean.Result;
import com.example.transation.txlcncommon.bean.entity.ConsumerEntity;
import com.example.transation.txlcncommon.bean.entity.ProviderEntity;
import com.example.transation.txlcncommon.bean.mapper.ConsumerMapper;
import com.example.transation.txlconsumer.feign.ProviderFeignClient;
import com.example.transation.txlconsumer.server.ConsumerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * 消费服务
 * @author: luojm
 * @create: 2020-08-31 11:05:10
 */
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, ConsumerEntity> implements ConsumerService {
    @Resource
    ProviderFeignClient providerFeignClient;

    @Override
    public Result provider() {
        return Result.success("调用 " + providerFeignClient.hello());
    }

    @Override
    @LcnTransaction
    @Transactional(rollbackFor = Exception.class)
    public Result txlcnTest(String s ) throws IOException {
        ConsumerEntity consumerEntity = new ConsumerEntity();
        consumerEntity.setAge(10);
        consumerEntity.setBalance(BigDecimal.TEN);
        consumerEntity.setName(s);
        save(consumerEntity);
        Result result = providerFeignClient.txlcnSave(s);
        ProviderEntity providerEntity = JSONObject.parseObject(result.getData().toString(), ProviderEntity.class);
        System.err.println(providerEntity);
        return Result.success(providerEntity);
    }
}
